<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom:10px">
      <el-select v-model="listQuery.classesId" class="filter-item" placeholder="请选择班次">
        <el-option
          v-for="item in classes"
          :key="item.classesId"
          :label="item.classesName"
          :value="item.classesId"
        />
      </el-select>
      <el-select v-model="listQuery.departmentId" class="filter-item" placeholder="请选择部门">
        <el-option
          v-for="item in departments"
          :key="item.departmentId"
          :label="item.departmentName"
          :value="item.departmentId"
        />
      </el-select>
      <el-input
        v-model="listQuery.staffName"
        placeholder="请输入员工名称"
        style="width: 200px;"
        class="filter-item"
      />
      <el-date-picker
        v-model="listQuery.date"
        align="right"
        type="date"
        placeholder="选择日期"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd"
      ></el-date-picker>

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="fetchData"
      >查询</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="60">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column align="center" label="部门名称">
        <template slot-scope="scope">{{ scope.row.departmentName }}</template>
      </el-table-column>
      <el-table-column label="员工名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.staffName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工编号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.staffId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.taskYear }}-{{ scope.row.taskMonth }}-{{ scope.row.taskDay }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考勤状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.state == '10000'?'未考勤': scope.row.state== '20000'? '考勤中': '考勤完成'}}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" width="300" align="center">
        <template slot-scope="{row,$index}">
          <el-row>
            <el-button size="mini" type="primary" @click="viewDetail(row,$index)">详情</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.row"
      @pagination="getTodayAttendances"
    />

    <el-dialog
      title="考勤详情"
      :visible.sync="deleteClassesStaffDailogVisible"
      width="80%"
      :before-close="handleClose"
    >
      <el-table :data="curTask.attendanceClassesTaskDetails" style="width: 100%">
        <el-table-column prop="specName" label="名称" width="180"></el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.state == '40000'">{{scope.row.stateName}}</el-tag>
            <el-tag type="danger" v-else-if="scope.row.state == '50000'">{{scope.row.stateName}}</el-tag>
            <el-tag type="info" v-else-if="scope.row.state == '60000'">{{scope.row.stateName}}</el-tag>
            <el-tag type="success" v-else-if="scope.row.state == '30000'">{{scope.row.stateName}}</el-tag>
            <el-tag v-else>{{scope.row.stateName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="value" label="打卡时间"></el-table-column>
        <el-table-column prop="checkTime" label="实际打卡时间"></el-table-column>
        <el-table-column label="抓拍">
          <template slot-scope="scope">
            <el-image
              ref="lazyImg"
              lazy
              class="vx-lazyLoad"
              :src="scope.row.facePath"
              :fit="fit"
              :preview-src-list="[scope.row.facePath]"
            >
              <div slot="placeholder" class="image-slot">
                <i class="el-icon-loading"></i>加载中
              </div>
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteClassesStaffDailogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDepartment,
  getClasses,
  getAttendanceTasks
} from "@/api/attendance";
import Pagination from "@/components/Pagination";
import { parseTime } from "@/utils";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  components: { Pagination },
  data() {
    return {
      listQuery: {
        page: 1,
        row: 10,
        classesId: "",
        staffName: "",
        date: "",
        departmentId: ""
      },
      classes: [],
      departments: [],
      list: null,
      listLoading: true,
      deleteClassesStaffDailogVisible: false,
      curTask: {},
      total: 0,
      temp: {
        classesId: "",
        departmentId: "",
        staffId: ""
      },
      rules: {},
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getClasses({
        page: 1,
        row: 50
      }).then(response => {
        this.classes = response.data;
        this.total = response.total;
        this.listLoading = false;
      });
      getAttendanceTasks(this.listQuery).then(response => {
        this.list = response.data;
        this.total = response.total;
        this.listLoading = false;
      });
      getDepartment().then(response => {
        this.departments = response.data;
        this.total = response.total;
        this.listLoading = false;
      });
    },
    getTodayAttendances() {
      getAttendanceTasks(this.listQuery).then(response => {
        this.list = response.data;
        this.total = response.total;
        this.listLoading = false;
      });
    },
    viewDetail(_row) {
      this.deleteClassesStaffDailogVisible = true;
      this.curTask = _row;
    }
  }
};
</script>
